﻿var map = null;
var points = [];
var shapes = [];
var center = null;

function LoadMap(latitude, longitude, onMapLoaded) {
    map = new VEMap('theMap');
    options = new VEMapOptions();
    options.EnableBirdseye = false;

    map.SetDashboardSize(VEDashboardSize.Small);

    if (onMapLoaded != null) map.onLoadMap = onMapLoaded;

    if (latitude != null && longitude != null) {
        center = new VELatLong(latitude, longitude);
    }
    map.LoadMap(center, null, null, null, null, null, null, options);
}

function LoadPin(LL, name, description) {
    var shape = new VEShape(VEShapeType.Pushpin,LL);

    shape.SetTitle("<span class=\"pintitle\">" + escape(name) + "</span>");

    if (description != undefined) {
        shape.SetDescription("<p class=\"pinDetails\">" +
		escape(description) + "</p>");
    }

    map.AddShape(shape);
    points.push(LL); 
    shapes.push(shape); 
}


function FindAddressOnMap(where) {
    var numberOfResults = 20;
    var setBestMapView = true;
    var showResults = true;

    map.Find("", where, null, null, null,
	numberOfResults, showResults, true, true,
	setBestMapView, callbackForLocation);
}

function callbackForLocation(layer, resultsArray, places,
hasMore, VEErrorMessage) {
    clearMap();

    if (places == null) return;

    $.each(places, function(i, item) {
        var description = "";
        if (item.Description !== undefined) {
            description = item.Description;
        }
        var LL = new VELatLong(item.LatLong.Latitude,
		item.LatLong.Longitude);
        LoadPin(LL, item.Name, description);
    });

    if (points.length > 1) {
        map.SetMapView(points);
    }

    if (points.length == 1) {
        $("#Latitude").val(points[0].Latitude);
        $("#Longitude").val(points[0].Longitude);
    }

}

function clearMap() {
    map.Clear();
    points = [];
    shapes = [];
}

function FindDinnersGivenLocation(where) {
    map.Find("", where, null, null, null, null, null, false,
	null, null, callbackUpdateMapDinners);
    //alert(new Date());
}


function callbackUpdateMapDinners(layer, resultsArray, places, hasMore, VEErrorMessage) {
    $("#dinnerList").empty();
    clearMap();
    var center = map.GetCenter();
    var path = appPath;
    $.post(path+"/Search/SearchByLocation",
	{
	    latitude: center.Latitude,
	    longitude: center.Longitude
	},

	function(dinners) {
	    $.each(dinners, function(i, dinner) {
	        var LL = new VELatLong(dinner.Latitude,
			dinner.Longitude, null, null);  //中国区的live地图不支持海拔设置，第三个参数为空，表示忽略海拔设置。如果设置为零，系统无法正常工作。

	        var RsvpMessage = "";

	        if (dinner.RSVPCount == 1)
	            RsvpMessage = "" + dinner.RSVPCount + "RSVP";
	        else
	            RsvpMessage = "" + dinner.RSVPCount + "RSVPS";
	        LoadPin(LL, '<a href="/Dinners/Details/' + dinner.DinnerID + '">'
			            + dinner.Title + '</a>',
			            "<p>" + dinner.Description + "</p>" + RsvpMessage);

	        $('#dinnerList').append(
	            $('<li/>').attr("class", "dinnerItem").append(
	                $('<a/>').attr("href", path+"/Dinners/Details/" + dinner.DinnerID).html(dinner.Title)).append(
	                "(" + RsvpMessage + ")"
	                )
	        );
	    });
	    if(points.length>1) map.SetMapView(points);
	    $(".dinnerItem").each(function(i, dinner) {
	        $(dinner).hover(
			function() { map.ShowInfoBox(shapes[i]); },
			function() { map.HideInfoBox(shapes[i]); }
			);
	    });
	}, "json");
}
